package cn.dinkevin.xui.util;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import cn.dinkevin.xui.R;
import cn.dinkevin.xui.XUI;


/**
 * Created by chengpengfei on 2017/5/8.
 * Copyright ©2017 juziwl, All Rights Reserved.
 */

public class ToastUtil {

    private static Toast mToast;

    /**
     * 调用系统的 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
     * @param message
     */
    public static void show(String message){
        show(message, Toast.LENGTH_SHORT);
    }


    /**
     * 调用系统的 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
     * @param message
     */
    public static void show(int message){
        show(message, Toast.LENGTH_SHORT);
    }


    /**
     * 显示 Toast 信息
     * @param text
     * @param duration
     */
    private static void show(String text,int duration){


        if(TextUtils.isEmpty(text)) return;
        View contentView = LayoutInflater.from(XUI.getInstance().getContext()).inflate(R.layout.xui_view_toast,null);
        TextView txt_title = contentView.findViewById(R.id.txt_toast_text);

        mToast = Toast.makeText(XUI.getInstance().getContext(),text,duration);
        mToast.setGravity(Gravity.CENTER,0,0);
        txt_title.setText(text);
        mToast.setView(contentView);
        mToast.show();
    }


    /**
     * 显示 Toast 信息
     * @param text
     * @param duration
     */
    private static void show(int text,int duration){

        cancel();

        View contentView = LayoutInflater.from(XUI.getInstance().getContext()).inflate(R.layout.xui_view_toast,null);
        TextView txt_title = contentView.findViewById(R.id.txt_toast_text);
        txt_title.setText(text);

        mToast = Toast.makeText(XUI.getInstance().getContext(),text,duration);
        mToast.setGravity(Gravity.CENTER,0,0);
        mToast.setView(contentView);
        mToast.show();
    }


    public static void cancel(){
        if(null != mToast){
            mToast.cancel();
            mToast = null;
        }
    }
}
